﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BaoJian
{
    public partial class searchresult : System.Web.UI.Page
    {

        public string skey = null;
        //public string SearchResultCount;
        protected void Page_Load(object sender, EventArgs e)
        {        
            if (HttpContext.Current.Request.QueryString["skey"] == null || (HttpContext.Current.Request.QueryString["skey"] == ""))//错误检测
            {
                Page.Response.Redirect("searchresult.aspx?skey=输入要搜索的关键字");
                return;
            }  
            else
            {
                skey = HttpContext.Current.Request.QueryString["skey"] as string;
                KeyWords.Text = skey;
            }

            string total = TotalNum.Value;
            if (total == "")
            {     
                string sql = "select count(*) as total from doc where " + FormatWhere();
                sql = sql.Replace("''", "'");
                System.Data.SqlClient.SqlDataReader dr = Maticsoft.DBUtility.DbHelperSQL.ExecuteReader(sql);
                int totalNum = 0;
                if (dr.Read())
                {
                    totalNum = (int)dr["total"];
                }
                dr.Close();
                TotalNum.Value = totalNum.ToString();
                AspNetPager1.RecordCount = totalNum;
            }
            else
            {
                AspNetPager1.RecordCount =  Convert.ToInt32(total);
            }
        }
        private void BindData()
        {
            //终于解决分页的条件问题
            string sql = " UP_GetRecordByPageOrder 'doc','sid,oid,pid,title,skey,summary ','sid','oid'," + AspNetPager1.PageSize.ToString() + "," + AspNetPager1.CurrentPageIndex.ToString() + ",1,0,'" + FormatWhere() + "'";
            DataSet ds = Maticsoft.DBUtility.DbHelperSQL.Query(sql);
            ListResult.DataSource = ds.Tables[0];
            ListResult.DataBind();
           
            AspNetPager1.CustomInfoHTML = "Page  <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex + "</b></font> of  " + AspNetPager1.PageCount;
            AspNetPager1.CustomInfoHTML += "&nbsp;&nbsp;搜索结果" + AspNetPager1.StartRecordIndex + "-" + AspNetPager1.EndRecordIndex;
        }   

        private string FormatWhere()
        {
            if (skey == null && skey == "")
                return " (1 = 1) ";
            string[] t = skey.Split(' ');        
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("(");
            for (int i = 0; i < t.Length; i++)
            {
                if (i == 0)
                {
                    sb.Append(" SKey like ''%" + t[i] + "%'' or Title like  ''%" + t[i] + "%'' ");
                }
                else
                {
                    sb.Append( " SKey like ''%" + t[i] + "%'' or Title like  ''%" + t[i] + "%'' ");
                }
            }

            sb.Append(")");

            return sb.ToString();
        }
        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }     
    }
}
